package com.ven.assists.simple.stategrid

import android.content.ComponentName
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import com.ven.assist.Assists
import com.ven.assist.ext.click
import com.ven.assist.ext.clickBack
import com.ven.assist.ext.clickScreen
import com.ven.assist.ext.clickScreen2
import com.ven.assist.ext.containsText
import com.ven.assist.ext.getBoundsInScreen
import com.ven.assist.step.StepCollector
import com.ven.assist.step.StepImpl
import com.ven.assist.step.StepManager
import com.ven.assist.ui.UIOperate
import com.ven.assists.simple.OverManager
import com.ven.assists.simple.step.Step
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class OpenStateGrid : StepImpl {

    private var clicked7: Boolean = false
    private var strDay: String = ""

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onImpl(collector: StepCollector) {
        collector.next(Step.STEP_1) {
            OverManager.log("启动网上国网")
            Intent().apply {
                addCategory(Intent.CATEGORY_LAUNCHER)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                component = ComponentName("com.sgcc.wsgw.cn", "com.sgcc.wsgw.cn.SplashActivity")
                Assists.service?.startActivity(this)
            }
            StepManager.execute(this::class.java, Step.STEP_2, delay = 20000)
        }.nextLoop(Step.STEP_2) {
            OverManager.log("检查页面广告页面")
            UIOperate.findByText("今日不再出现").forEach {
                it.clickBack()
                StepManager.execute(this::class.java, Step.STEP_3)
                return@nextLoop true
            }
            if (0f == it.loopSurplusSecond) {
                StepManager.execute(this::class.java, Step.STEP_3)
                return@nextLoop true
            }
            false
        }.nextLoop(Step.STEP_3) {
            OverManager.log("检查是否已打开网上营业厅：\n剩余时间=${it.loopSurplusSecond}秒")
            UIOperate.findByText("网上营业厅").forEach {
                val screen = it.getBoundsInScreen()
                OverManager.log("已打开网上营业厅")
                it.clickScreen()
                StepManager.execute(this::class.java, Step.STEP_4)
                return@nextLoop true
            }
            false
        }.nextLoop(Step.STEP_4) {
            OverManager.log("电量电费")
            UIOperate.findByText("电量电费").forEach {
                it.clickScreen2()
                StepManager.execute(this::class.java, Step.STEP_6, delay = 10000)
                return@nextLoop true
            }
            false
        }.nextLoop(Step.STEP_5) {
            OverManager.log("切换户号")
            UIOperate.clickScreen(911F,367F, 10)//切换户号
            StepManager.execute(this::class.java, Step.STEP_61, delay = 10000)
//            UIOperate.findByText("切换户号").forEach {
//                it.clickScreen()
//                return@nextLoop true
//            }
            true
        }.nextLoop(Step.STEP_61) {
            OverManager.log("切换户号")
            UIOperate.clickScreen(911F,367F, 10)//切换户号
            StepManager.execute(this::class.java, Step.STEP_61, delay = 10000)
//            UIOperate.findByText("切换户号").forEach {
//                it.clickScreen()
//                return@nextLoop true
//            }
            true
        }.nextLoop(Step.STEP_6) {
            OverManager.log("日用电量")
            //UIOperate.clickScreen(911F,367F, 10)//切换户号
            UIOperate.clickScreen(708F, 716F, 10) //日电量
            //UIOperate.clickScreen(120F,180F, 10)//返回<
            StepManager.execute(this::class.java, Step.STEP_7, delay = 5000)
            true
        }.nextLoop(Step.STEP_7) {
            OverManager.log("尖峰平谷采集-展开昨天")
            val day = getYesterday()
            UIOperate.getAllNodes().forEach {
                val node = it.containsText(day)
                if (node != null) {
                    clicked7 = node.click()
                    return@forEach
                }
            }
            if (clicked7) {
                strDay += "$day>"
                StepManager.execute(this::class.java, Step.STEP_8)
            } else {
                StepManager.execute(this::class.java, Step.STEP_9)
            }
            true
        }.nextLoop(Step.STEP_8) {
            OverManager.log("尖峰平谷采集-采集昨天")
            UIOperate.getAllNodes().forEach {
                val node = it.containsText("用电：")
                if (node != null) {
                    strDay += node.text.toString() + ";"
                }
            }
            StepManager.execute(this::class.java, Step.STEP_9)
            true
        }.nextLoop(Step.STEP_9) {
            OverManager.log("尖峰平谷采集-展开前天")
            val day = getAvantHier()
            UIOperate.getAllNodes().forEach {
                val node = it.containsText(day)
                if (node != null) {
                    clicked7 = node.click()
                    return@forEach
                }
            }
            if (clicked7) {
                strDay += "$day>"
                StepManager.execute(this::class.java, Step.STEP_10)
            } else {
                StepManager.execute(this::class.java, Step.STEP_100)
            }
            true
        }.nextLoop(Step.STEP_10) {
            OverManager.log("尖峰平谷采集-采集前天")
            UIOperate.getAllNodes().forEach {
                val node = it.containsText("用电：")
                if (node != null) {
                    strDay += node.text.toString() + ";"
                }
            }
            StepManager.execute(this::class.java, Step.STEP_100)
            true
        }.nextLoop(Step.STEP_100) {
            OverManager.log(strDay)
            false
        }
//        .nextLoop(Step.STEP_9) {
//            val x = ScreenUtils.getAppScreenWidth() / 2F
//            val distance = ScreenUtils.getAppScreenHeight() / 2F
//            val startY = distance + distance / 2F
//            val endY = distance - distance / 2F
//            OverManager.log("滑动：$x/$startY,$x/$endY")
//            val delay = UIOperate.gesture(
//                floatArrayOf(x, startY), floatArrayOf(x, endY), 0, 2000L
//            )
//            StepManager.execute(this::class.java, Step.STEP_8, Assists.Config.defaultStepDelay + delay)
//            false
//        }s
    }

    private fun yesterday(collector: StepCollector, day: String): String {
        collector.next(Step.STEP_100) {
            OverManager.log("尖峰平谷采集-昨日")
            UIOperate.getAllNodes().forEach {
                val node = it.containsText(day)
                if (node != null) {
                    node.click()
                    return@forEach
                }

            }
            StepManager.execute(this::class.java, Step.STEP_101)
        }.nextLoop(Step.STEP_101) {
            UIOperate.getAllNodes().forEach {
                val node = it.containsText("用电：")
                if (node != null) {
                    strDay += node.text
                }
            }
            true
        }
        return strDay
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private final fun getYesterday(): String {
        val current = LocalDateTime.now().minusDays(1)
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        return current.format(formatter)
    }

    private final fun getAvantHier(): String {
        val current = LocalDateTime.now().minusDays(2)
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        return current.format(formatter)
    }
}



